﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MainService
{
    public class MyLog
    {
        private static object _helper = new object();
        private static MyLog _instance;
        private static object _fileLock = new object();

        private MyLog()
        { }

        public static MyLog GetInstance()
        {
            lock (_helper)
            {
                if (_instance == null)
                {
                    _instance = new MyLog();
                }
            }
            return _instance;
        }

        /// <summary>
        /// 发送Mail日志
        /// </summary>
        /// <param name="message">信息</param>
        public void WriteEmailLog(string message)
        {
            string filePath = System.AppDomain.CurrentDomain.BaseDirectory +DateTime.Now.ToString("yyyy-MM-dd")+ ".log";
            StreamWriter sw = null;
            FileStream fs = null;
            lock (_fileLock)
            {
                if (!File.Exists(filePath))
                {
                    fs = System.IO.File.Create(filePath);
                    sw = new StreamWriter(fs, Encoding.UTF8);
                    sw.WriteLine("---------------------------------------------");
                    sw.WriteLine(message);
                    sw.Flush();
                    sw.Close();
                }
                else
                {
                    fs = new FileStream(filePath, FileMode.Append);
                    sw = new StreamWriter(fs, Encoding.UTF8);
                    sw.WriteLine("---------------------------------------------");
                    sw.WriteLine(message);
                    sw.Flush();
                    sw.Close();
                }
            }
        }
    }
}
